import 'dart:collection';
import 'dart:convert';

import 'package:get/get.dart';
import 'package:meePet/bean/FoundTabListData.dart';
import 'package:meePet/bean/RolePageData.dart';
import 'package:meePet/net/RequestManager.dart';
import 'package:meePet/utils/LogUtils.dart';
import 'package:meePet/utils/ext/IntExt.dart';

import '../../base/LiveData.dart';

class RoleListFragmentVM extends GetxController {
  LiveData<List<RoleData>?> roleListLd = LiveData();
  FoundTab? curTab;

  void getRoleListByCondition(
      {bool isRefresh = true, bool showLoading = false}) {
    _requestRolePageList();
  }

  void _requestRolePageList() {
    HashMap<String, dynamic> params = HashMap();
    params["pageNo"]= "1";
    params["pageSize"]= "50";
    params["tabId"]= "${curTab?.id}";
    params["tabAlias"]= "${curTab?.alias}";

    RequestManager.getClient()
        .rolePageList(params)
        .then((result) => {_dealWithRoleListData(result)});
  }

  void _dealWithRoleListData(RolePageData data) {
    if (data.code?.isHttpSuccess() == true) {
      String jsonStr = json.encode(data.data);
      LogUtils.i("网络获取json:${curTab?.alias}：$jsonStr");
      roleListLd.post(data.data?.list);
    } else {
      LogUtils.i("网络获取错误：${data.code}:${data.msg}");
    }
  }

  @override
  void onClose() {
    roleListLd.doDestroy();
    super.onClose();
  }
}
